package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.DiseCategory;
import com.foreknow.service.DiseCategoryService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "DiseCategoryController", description = "疾病分类查询")
@RestController
@RequestMapping("/diseCategoryController")
public class DiseCategoryController {
    /**
     * 查询所有疾病分类
     *
     * @return
     */
    @Autowired
    private DiseCategoryService diseCategoryService;

    @RequestMapping(value = "/selectDiseCategoryList", method = RequestMethod.POST)
    @ResponseBody
    public String selectDiseCategoryList(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        Integer pageNum = (Integer) para.get("pageNum");
        Integer pageSize = (Integer) para.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<DiseCategory> list = diseCategoryService.selectDiseCategoryList();
        //得到分页的结果对象
        PageInfo<DiseCategory> personPageInfo = new PageInfo<>(list);
        HashMap<String, Object> hs = new HashMap<>();
        if (list != null) {
            hs.put("data", list);
            hs.put("personPageInfo", personPageInfo);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @ApiOperation("根据 sequencno进行模糊查询")
    @RequestMapping(value = "/selectDiseCategoryBydisecatgory", method = RequestMethod.POST)
    @ResponseBody
    public String selectDiseCategoryBydisecatgory(@RequestBody DiseCategory disecategory) throws JsonProcessingException {
        disecategory.setDicacode(disecategory.getDicaname());

        Integer pageNum = disecategory.getPageNum();
        Integer pageSize = disecategory.getPageSize();
        // //调用Service 的方法
        System.out.println(disecategory);
        PageHelper.startPage(pageNum, pageSize);
        List<DiseCategory> diseCategory3 = diseCategoryService.selectDiseCategoryBydisecatgory(disecategory);
        PageInfo<DiseCategory> personPageInfo = new PageInfo<>(diseCategory3);
        HashMap<String, Object> hs = new HashMap<>();
        if (diseCategory3 != null) {
            hs.put("data", diseCategory3);
            hs.put("meta", new Constance("msg", 200));
            hs.put("personPageInfo", personPageInfo);
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    /**
     * 添加疾病分类
     *
     * @param diseCategory
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    @ResponseBody
    public String insertSelective(@RequestBody DiseCategory diseCategory) throws JsonProcessingException {
//        DiseCategory diseCategory = (DiseCategory) para.get("diseCatgory");
        DiseCategory diseCategory1 = diseCategory;
        //调用Service 的方法
        //添加
        HashMap<String, Object> hs = new HashMap<>();
        Integer bb = diseCategoryService.insertSelective(diseCategory);

        if (bb != 0) {
            hs.put("data", bb);
            hs.put("meta", new Constance("添加成功", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    /**
     * 修改
     *
     * @param diseCategory
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/updateByPrimaryKey", method = RequestMethod.POST)
    @ResponseBody
    public String updateByPrimaryKey(@RequestBody DiseCategory diseCategory) throws JsonProcessingException {
        DiseCategory diseCategory1 = diseCategory;
        //调用Service 的方法
        int bb = diseCategoryService.updateByPrimaryKey(diseCategory);
        HashMap<String, Object> hs = new HashMap<>();
        if (bb != 0) {
            hs.put("data", bb);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    /**
     * 根据sequenceno查询疾病分类
     *
     * @param para
     * @return
     * @throws JsonProcessingException
     */
    @ApiOperation("通过Id查询")
    @RequestMapping(value = "/selectdisecategory", method = RequestMethod.POST)
    @ResponseBody
    public String selectdisecategory(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        Integer sequenceno = (Integer) para.get("sequenceno");
        //调用Service 的方法
        DiseCategory diseCategory = diseCategoryService.selectdisecategory(sequenceno);

        HashMap<String, Object> hs = new HashMap<>();
        if (diseCategory != null) {
            hs.put("data", diseCategory);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    /**
     * 根据sequencno进行逻辑删除
     *
     * @param diseCategory
     * @return
     * @throws JsonProcessingException
     */
    @ApiOperation("根据 sequencno进行逻辑删除")
    @RequestMapping(value = "/updateDiseategory", method = RequestMethod.PUT)
    @ResponseBody
    public String updateDiseategory(@RequestBody DiseCategory diseCategory) throws JsonProcessingException {
// //调用Service 的方法
        int sequencno = diseCategory.getSequenceno();
        int aaa = diseCategoryService.updateDiseategory(sequencno);
        HashMap<String, Object> hs = new HashMap<>();
        if (aaa != 0) {
            hs.put("data", aaa);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

}
